package cn.itcast.jedis.utils;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;

/*
JedisPool工具类:
  加载配置文件,配置连接池的参数
  提供获取连接对象的方法
 */
public class JedisPoolUtils {
    private static JedisPool jedisPool;
    //加载配置文件,配置连接池的参数
    static {
        //读取配置文件
        InputStream is = JedisPoolUtils.class.getClassLoader().getResourceAsStream("jedis.properties");
        Properties properties = new Properties();
        try {
            properties.load(is);
            JedisPoolConfig config = new JedisPoolConfig();
            config.setMaxIdle(Integer.parseInt(properties.getProperty("maxIdle")));
            config.setMaxTotal(Integer.parseInt(properties.getProperty("maxTotal")));


            jedisPool=new JedisPool(config,properties.getProperty("host"),Integer.parseInt(properties.getProperty("port")));

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //获取连接对象的方法
    public static  Jedis getJedis(){
        Jedis jedis = jedisPool.getResource();
        return jedis;
    }

}
